﻿using System;
using System.Collections;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;


namespace AMS.Web.BackStageManagement
{
    public partial class UserGroup : Page
    {
        CastleAccess acc = new CastleAccess();
        readonly BaseDataManager help = new BaseDataManager();
        private string functionString = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Cookies["LoginUserId"] == null)
                {
                    Response.Redirect("../Default.aspx");
                }
                
                ShowTree();
            }
        }

        public void ShowTree()
        {
            TreeView_UserGroup.Nodes.Clear();

            TreeNode node_0 = new TreeNode();
            node_0.ImageUrl = "../../Images/calendar.gif";
            node_0.Text = "部门管理" + "<a herf='#'></a>";
            node_0.Value = "0";
            node_0.Text += "&nbsp;&nbsp;<a target='_self' href='UserGroupInsert.aspx?id=" +
                publ.GetUrlToSend(node_0.Value.ToString().Trim()) + "'><img style='width:16px;height:16px' src='Images/msnadd.gif' alt='新增部门'/></a>";
            node_0.SelectAction = TreeNodeSelectAction.Expand;
            node_0.Expanded = true;
            TreeView_UserGroup.Nodes.Add(node_0);
            ViewState["ds"] =
            acc.GetDataSetBySql("SELECT m.* FROM Frame_UserGroup m   WHERE 1 = 1  and  m.ParentId is null ");
            DataSet ds = (DataSet)this.ViewState["ds"];
            DataView dvTree = new DataView(ds.Tables[0]);

            foreach (DataRowView Row in dvTree)
            {
                TreeNode Node = new TreeNode();
                Node.Text = Row["Name"].ToString().Trim();
                Node.Value = Row["id"].ToString().Trim();
                Node.ImageUrl = "Images/chart_bar.gif";
                Node.ToolTip = "";
                Node.Text += "&nbsp;&nbsp;<a target='_self' href='UserGroupInsert.aspx?id=" +
                    publ.GetUrlToSend(Node.Value.ToString().Trim()) + "'><img style='width:16px;height:16px' src='Images/msnadd.gif' alt='新增子部门'/></a>";
                Node.Text += "&nbsp;&nbsp;<a target='_self' href='UserGroupUpdate.aspx?id=" +
                    publ.GetUrlToSend(Node.Value.ToString().Trim()) + "'><img style='width:16px;height:16px' src='Images/edit.gif' alt='编辑'/></a>";
                Node.Text += "&nbsp;&nbsp;<a target='_self' href='UserImport.aspx?id=" +
                    publ.GetUrlToSend(Node.Value.ToString().Trim()) + "'><img style='width:16px;height:16px' src='Images/wzmale.gif' alt='设置用户'/></a>";
                node_0.ChildNodes.Add(Node);
                Node.Expanded = true;
                AddTree(Node.Value, Node);
            }

        }

        public void AddTree(String parentId, TreeNode pNode)
        {
            IList objParentList = acc.GetObjectList(new VitCode.Castle.Entities.UserGroup().GetType().Name, "Id", parentId.Trim());
            if (objParentList != null && objParentList.Count > 0)
            {
                var objParentUserGroup = (VitCode.Castle.Entities.UserGroup)objParentList[0];

                IList objList = acc.GetObjectList(new VitCode.Castle.Entities.UserGroup().GetType().Name, "Parent",
                                                  objParentUserGroup.Id.ToString().Trim());
                if (objList != null && objList.Count > 0)
                {
                    foreach (VitCode.Castle.Entities.UserGroup currentUserGroup in objList)
                    {
                        TreeNode node = new TreeNode();


                        node.Value = currentUserGroup.Id.ToString().Trim();
                        node.Text = currentUserGroup.Name.Trim() + "<a herf='#'></a>";
                        node.ToolTip = "";
                        node.Text += "&nbsp;&nbsp;<a target='_self' href='UserGroupInsert.aspx?id=" +
                        publ.GetUrlToSend(node.Value.ToString().Trim()) + "'><img style='width:16px;height:16px' src='Images/msnadd.gif' alt='新增子部门'/></a>";
                        node.Text += "&nbsp;&nbsp;<a target='_self' href='UserGroupUpdate.aspx?id=" +
                        publ.GetUrlToSend(node.Value.ToString().Trim()) + "'><img style='width:16px;height:16px' src='Images/edit.gif' alt='编辑'/></a>";
                        node.Text += "&nbsp;&nbsp;<a target='_self' href='UserImport.aspx?id=" +
                        publ.GetUrlToSend(node.Value.ToString().Trim()) + "'><img style='width:16px;height:16px' src='Images/wzmale.gif' alt='设置用户'/></a>";
                        pNode.ChildNodes.Add(node);
                        node.Expanded = true;
                        node.ImageUrl = "../../Images/notes.gif";


                        AddTree(currentUserGroup.Id.ToString().Trim(), node);    //再次递归
                    }
                }
            }

        }

        /// <summary>
        /// 得到所选择的节点,调用自身函数循环遍历TreeView
        /// </summary>
        /// <param name="node"></param>
        /// <returns>返回FunctionString字符串</returns>
        public string GetTreeCheck(TreeNode node)
        {

            foreach (TreeNode treenode in node.ChildNodes)
            {
                if (treenode.Checked)
                {
                    string va = treenode.Value;
                    functionString += ", " + va + "";
                }

                GetTreeCheck(treenode);

            }
            return functionString;
        }
 

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string strUserGroupCodeAll = GetTreeCheck(TreeView_UserGroup.Nodes[0]);
            bool access = false;
            if (string.IsNullOrEmpty(strUserGroupCodeAll))
            {
                Alert.MessageBox("请选择要删除的部门!");
                return;
            }
            
            string[] ss = strUserGroupCodeAll.Split(',');


            for (int i = 0; i < ss.Length; i++)
            {

                if (!string.IsNullOrEmpty(ss[i]))
                {
                    IList list = help.CheckExist("From User where UserGroupId='" + ss[i].Trim() + "'");
                    if (list != null && list.Count > 0)
                    {
                        Alert.MessageBox("该部门下有用户，不能删除!");
                        return;

                    }
                    access = help.DeleteObjById(new VitCode.Castle.Entities.UserGroup(), ss[i].Trim());
                  
                }

            }
            if (access)
            {
                Alert.MessageBox("删除成功!");
            }

            ShowTree();
        }

    }
}
